package com.bltec.emergency.mapper;

import com.bltec.emergency.model.Permission;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface PermissionMapper {

    @Insert("insert into sys_permission (parent_id,name,percode,href,create_by,create_time,enable) values(#{parentId},#{name},#{percode},#{href},#{createBy},#{createTime},#{enable})")
    boolean insert(Permission permission);

    @Update("update sys_permission set parent_id=#{parentId},name=#{name},percode=#{percode},href=#{href},update_by=#{updateBy},update_time=#{updateTime} where id=#{id} and enable = 1")
    boolean updatePermissionById(Permission permission);

    @Update("update sys_permission set enable=0 where id=#{id} and enable = 1")
    boolean setDisableById(Long id);

    @Select("select * from sys_permission where name =#{name} and enable = 1")
    Permission getPermissionByName(String name);

    @Select("select * from sys_permission where enable=1")
    List<Permission> getAll();

    @Select("select id,parent_id,name from sys_permission where enable=1")
    List<Permission> getPermissionIdParentIdName();

    @Select("select * from sys_permission where id=#{id} and enable = 1")
    Permission getPermissionById(Long id);


    @Select("select p.* from sys_role r left outer join sys_role_permission rp on r.id=rp.role_id left outer join sys_permission p on rp.permission_id = p.id where r.id=#{roleId} and p.enable=1 and r.enable=1")
    List<Permission> getAllPermissionByRoleId(Long roleId);
}
